/* eslint-disable max-classes-per-file */
import { G6, ifs } from '../config';
import B_addEdge from './add-edge';
import B_edgeDBclick from './edge-dblclick';
import B_hoverEdge from './hover-edge';
import B_hoverNode from './hover-node';
import B_keyboard from './keyboard';

class T_modes {
  // 自定义行为
  private _behavors = {
    'hover-node': B_hoverNode,
    'hover-edge': B_hoverEdge,
    'add-edge': B_addEdge,
    'edge-dbclick': B_edgeDBclick,
    B_keyboard
  };

  private __behavors = {};

  // 默认行为(不可编辑)
  private _default: Array<any> = [
    'drag-canvas', // 拖拽画布
    'zoom-canvas', // 缩放画布
    { type: 'click-select', multiple: false } // 选择节点
  ];

  // 编辑行为
  private _edit: Array<any> = [
    'drag-canvas', // 拖拽画布
    'zoom-canvas', // 缩放画布
    'drag-node',
    'brush-select', // 按shift 框选
    'click-select', // 选择节点 ctrl | shift 多选

    // // 自定义
    'hover-node',
    'hover-edge',
    'edge-dbclick',
    'B_keyboard'
  ];

  private addEdge: Array<any> = ['add-edge'];

  private __modes = {
    // default: this._default,
    // other: this._other,
    // edit: this._edit
  };

  constructor(compID: string) {
    // 重组自定义行为以compID区分 组件重复使用相同行为时不产生冲突
    const objs = {
      behavors: this._behavors,
      default: this._default,
      addEdge: this.addEdge,
      edit: this._edit
    };
    for (const k in objs) {
      const o: Array<any> = [];
      for (const key in objs[k]) {
        let kn: string = `${compID}_${objs[k][key]}`;
        if (k === 'behavors') {
          kn = `${compID}_${key}`;

          this.__behavors[kn] = objs[k][key];
        } else {
          // 是否自定义行为
          kn = `${compID}_${objs[k][key]}`;
          if (this.__behavors[kn]) {
            o.push(kn);
          } else {
            o.push(objs[k][key]);
          }
        }
      }
      if (k !== 'behavors') {
        this.__modes[k] = o;
      }
    }
  }

  get behavors(): any {
    return this.__behavors;
  }

  get modes(): any {
    return this.__modes;
  }
}
class T_graphInit {
  private GT_compBoxInfo: ifs.I_compBoxInfo;

  private GT_compBox: ifs.I_compBox;

  private G_compID: string;

  private GT_graph: any;

  constructor(graph: any, compBoxInfo: ifs.I_compBoxInfo) {
    this.GT_compBoxInfo = compBoxInfo;
    this.GT_graph = graph;
    this.GT_compBox = this.GT_compBoxInfo.TcompBox;
    this.G_compID = this.GT_compBoxInfo.compID;
  }

  public initBehavors(Tmodel: T_modes) {
    // getDefaultCfg会被自动执行
    const getDefaultCfg = () => {
      const obj: any = {
        isKeyboardEnabled: true, // 注入变量 在事件中可以使用this访问
        compBox: this.compBox,
        compID: this.compID
      };
      return obj;
    };

    for (const key in Tmodel.behavors) {
      const L_behavors = Tmodel.behavors[key];
      // 注入方法
      L_behavors.getDefaultCfg = getDefaultCfg;
      G6.registerBehavior(key, L_behavors);
    }
  }

  get compBox(): ifs.I_compBox {
    return this.GT_compBox;
  }

  get compID(): string {
    return this.G_compID;
  }

  public initGraphOn() {
    // 画布事件
    // 使用了 'brush-select' , 'click-select' 或 'lasso-select' Behavior 且选中元素发生变化时，该事件被触发
    // 选择事件删除按钮控制
    this.GT_graph.on('nodeselectchange', e => {
      let L_items = [];
      if (e.selectedItems[`edges`] && Array.isArray(e.selectedItems.edges)) {
        L_items = L_items.concat(e.selectedItems[`edges`]);
      }
      if (e.selectedItems[`nodes`] && Array.isArray(e.selectedItems.nodes)) {
        L_items = L_items.concat(e.selectedItems[`nodes`]);
      }

      if (L_items.length > 0) {
        this.GT_compBox.getCompMergeName(
          this.G_compID,
          `graphToolbar`
        ).setBtnDisabled(`delete`, false);
      } else {
        this.GT_compBox.getCompMergeName(
          this.G_compID,
          `graphToolbar`
        ).setBtnDisabled(`delete`, true);
      }
    });
    // 撤销事件按钮状态控制
    this.GT_graph.on('stackchange', e => {
      if (e.undoStack.length > 0) {
        this.GT_compBox.getCompMergeName(
          this.G_compID,
          `graphToolbar`
        ).setBtnDisabled(`undo`, false);
      } else {
        this.GT_compBox.getCompMergeName(
          this.G_compID,
          `graphToolbar`
        ).setBtnDisabled(`undo`, true);
      }
      if (e.redoStack.length > 0) {
        this.GT_compBox.getCompMergeName(
          this.G_compID,
          `graphToolbar`
        ).setBtnDisabled(`redo`, false);
      } else {
        this.GT_compBox.getCompMergeName(
          this.G_compID,
          `graphToolbar`
        ).setBtnDisabled(`redo`, true);
      }
    });
    // 节点点击事件
    this.GT_graph.on(`node:click`, e => {
      // 向子组件发送点击事件
      this.GT_compBox.getCompMergeName(
        this.G_compID,
        `oFlowchart`
      ).onNodeSelect(e);
    });

    // 画布点击事件
    this.GT_graph.on(`canvas:click`, e => {
      // 向子组件发送点击事件
      this.GT_compBox.getCompMergeName(
        this.G_compID,
        `oFlowchart`
      ).onCanvasClick(e);
    });

    // 边点击事件
    this.GT_graph.on(`edge:click`, e => {
      this.GT_compBox.getCompMergeName(
        this.G_compID,
        `graphToolbar`
      ).setBtnDisabled(`delete`, false);
      // 向子组件发送点击事件
      this.GT_compBox.getCompMergeName(
        this.G_compID,
        `oFlowchart`
      ).onEdgeSelect(e);
    });
    // 状态改变之后
    this.GT_graph.on(`afteritemstatechange`, e => {
      if (e.state === `selected`) {
        // 向command类注册点击item
        this.GT_compBox.getCompMergeName(
          this.G_compID,
          `editor`
        ).initSelectedItems();
      }
    });
    // 状态改变之后
    this.GT_graph.on(`node:mousedown`, e => {
      // 右键
      if (e.originalEvent.button !== 2) {
        return;
      }
      // 向command类注册点击item
      this.GT_compBox.getCompMergeName(
        this.G_compID,
        `oFlowchart`
      ).onNodeRightClick(e);
    });
    // 调用 graph.render / graph.read 方法之后触发
    this.GT_graph.on(`afterrender`, () => {
      // 向command类注册点击item
      this.GT_compBox.getCompMergeName(
        this.G_compID,
        `oFlowchart`
      ).onAfterRender(this.GT_graph);
    });
  }
}
export { T_graphInit, T_modes };
